﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;

using Gift.DAL;
using Gift.Model;
using Gift.Model.ENUM;

namespace WebSite.admin.warehouse
{
    public partial class warehouseList : BaseAdmin
    {
        readonly DALWarehouse theWarehouse = new DALWarehouse();
        protected void Page_Load(object sender, EventArgs e)
        {


            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(ERule.WarehouseManager);
                this.DeleletWarehouse();
                this.BindGrid();

            }
        }
        private void BindGrid()
        {


            DataTable table = theWarehouse.GetList("").Tables[0];
            Grid1.DataSource = table;
            Grid1.DataBind();
            Grid1.SelectedRowIndexArray = new int[] { };

        }



        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;

        }

        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            DataTable table = theWarehouse.GetList("").Tables[0];
            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);
            Grid1.DataSource = view1;
            Grid1.DataBind();
        }

        void DeleletWarehouse()
        {
            if (Request.QueryString["action"] == "del")
            {
                theWarehouse.Delete(int.Parse(Request.QueryString["id"]));
                ExtAspNet.Alert.Show("删除仓库成功！");
            }
        }


        protected void btnBrachDeleted_Click1(object sender, EventArgs e)
        {

            int selectedCount = this.Grid1.SelectedRowIndexArray.Length;
            StringBuilder sWarehouse = new StringBuilder();
            for (int i = 0; i < selectedCount; i++)
            {
                int rowIndex = this.Grid1.SelectedRowIndexArray[i];
                if (i == selectedCount - 1)
                {
                    sWarehouse.Append(this.Grid1.DataKeys[rowIndex][0].ToString());
                }
                else
                {
                    sWarehouse.Append(this.Grid1.DataKeys[rowIndex][0].ToString() + ",");
                }
            }
            if (sWarehouse.Length > 0)
            {
                theWarehouse.DeleteList(sWarehouse.ToString());
            }
            ExtAspNet.Alert.Show("批量删除仓库成功！");
            this.BindGrid();
        }

        protected void btnAddWarehouse_Click(object sender, EventArgs e)
        {
            Response.Redirect("warehouse.aspx");
        }

      
    }
}